{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 04\n",
    "\n",
    "# 集合运算\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "410c213c-266c-4b3c-9836-fc3529290170",
   "metadata": {},
   "source": [
    "该代码定义了三个集合$A$、$B$和$C$，并对这些集合进行了并集、交集和差集的运算，展示集合之间的关系。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **集合定义**：\n",
    "   - 定义集合$A$为奇数集合，包含元素$\\{1, 3, 5\\}$。\n",
    "   - 定义集合$B$为偶数集合，包含元素$\\{2, 4, 6\\}$。\n",
    "   - 定义集合$C$为小于$4$的自然数集合，包含元素$\\{1, 2, 3\\}$。\n",
    "\n",
    "2. **集合运算**：\n",
    "   - **并集**：如$A \\cup B$和$A \\cup C$，计算两个集合中所有元素的集合。并集的计算公式为：\n",
    "\n",
    "     $$\n",
    "     A \\cup B = \\{x \\mid x \\in A \\text{ or } x \\in B\\}\n",
    "     $$\n",
    "\n",
    "   - **交集**：如$A \\cap B$和$A \\cap C$，表示两个集合的公共元素集合。交集的计算公式为：\n",
    "\n",
    "     $$\n",
    "     A \\cap B = \\{x \\mid x \\in A \\text{ and } x \\in B\\}\n",
    "     $$\n",
    "\n",
    "   - **差集**：差集如$A - B$、$A - C$和$C - A$，表示从第一个集合中移除所有属于第二个集合的元素。差集的计算公式为：\n",
    "\n",
    "     $$\n",
    "     A - B = \\{x \\mid x \\in A \\text{ and } x \\notin B\\}\n",
    "     $$\n",
    "\n",
    "### 示例运算结果\n",
    "\n",
    "- **$A \\cup B$**：所有奇数与偶数的集合$\\{1, 2, 3, 4, 5, 6\\}$。\n",
    "- **$A \\cap B$**：$A$和$B$没有共同元素，因此交集为空集$\\emptyset$。\n",
    "- **$A - C$**：从$A$中移除所有$C$中的元素，结果为$\\{5\\}$。\n",
    "\n",
    "### 总结\n",
    "\n",
    "该代码通过基本集合操作展示了集合论的核心概念。集合的并集、交集和差集运算在数据处理、关系建模和数学理论中具有广泛应用，是分析数据集及其关系的基本方法。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74083466-f726-4d29-82b0-b3f91507825c",
   "metadata": {},
   "source": [
    "## 定义集合A：奇数集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7ece2790-273e-4d11-871b-d1b1b3dbb15e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 3, 5}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = set([1, 3, 5])  # 定义集合A\n",
    "A  # 输出集合A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a53501dc-2fd8-4050-a619-f7e0f4ee89f4",
   "metadata": {},
   "source": [
    "## 定义集合B：偶数集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "424a8fd2-cf52-4064-b8b3-677c708a4cd6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 4, 6}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = set([2, 4, 6])  # 定义集合B\n",
    "B  # 输出集合B"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac4a188b-1ec8-4232-b981-3854646faaa2",
   "metadata": {},
   "source": [
    "## 定义集合C：小于4的数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d6271944-af6b-4770-9b5f-dfc684f2d352",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = set([1, 2, 3])  # 定义集合C\n",
    "C  # 输出集合C"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfe277d5-d8e2-433c-9f97-4026ad338f7e",
   "metadata": {},
   "source": [
    "## 集合A与B的并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "295b9f0f-267e-4a5d-b4c6-a897a2f9f8db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_union_B = A.union(B)  # 计算集合A和B的并集\n",
    "A_union_B  # 输出A和B的并集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee4e08d2-2587-46da-be16-8e8eafad6878",
   "metadata": {},
   "source": [
    "## 集合A与B的交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d49d637e-9f4d-43f6-96d1-b06223392e9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_meet_B = A.intersection(B)  # 计算集合A和B的交集\n",
    "A_meet_B  # 输出A和B的交集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4283d4ec-d605-4df9-aa19-cc4c5bbf2d70",
   "metadata": {},
   "source": [
    "## 集合A减去集合B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ccdbc954-c84f-4915-90e8-58f0bec716b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 3, 5}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_minus_B = A.difference(B)  # 计算集合A减去B的差集\n",
    "A_minus_B  # 输出A减去B的差集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3876bcc-1f66-4cc7-8ab3-9b38e92e64ae",
   "metadata": {},
   "source": [
    "## 集合A与C的并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "260a134b-bde9-44bf-bdf1-958a7b5147a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 5}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_union_C = A.union(C)  # 计算集合A和C的并集\n",
    "A_union_C  # 输出A和C的并集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9e56e21-d0e9-4a55-a78d-8fe9470398e9",
   "metadata": {},
   "source": [
    "## 集合A与C的交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7b81c2d5-ba42-4b41-a46a-cca7d83c1447",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 3}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_meet_C = A.intersection(C)  # 计算集合A和C的交集\n",
    "A_meet_C  # 输出A和C的交集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef537d45-1215-4b90-8ad8-73aa7d69dc80",
   "metadata": {},
   "source": [
    "## 集合A减去集合C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e9aad00b-ff58-48a3-ae0a-b2fb9c7b4d08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{5}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_minus_C = A.difference(C)  # 计算集合A减去C的差集\n",
    "A_minus_C  # 输出A减去C的差集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fac6536-3b2e-4437-b378-221f18e964a1",
   "metadata": {},
   "source": [
    "## 集合C减去集合A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "59478f53-9fe4-443b-bf4a-7272e864671e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C_minus_A = C.difference(A)  # 计算集合C减去A的差集\n",
    "C_minus_A  # 输出C减去A的差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
